﻿using MediatR;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.Process;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Get.API.Application.Commands.Technologys
{
    public class ProcessShowHandler : IRequestHandler<ProcessShowCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        /// <summary>
        /// 工作中心仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Process> processRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ProcessShowHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processRepository"></param>
        /// <param name="logger"></param>
        public ProcessShowHandler(IBaseRepositoryGet<Process> processRepository, ILogger<ProcessShowHandler> logger)
        {
            this.processRepository = processRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(ProcessShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Search };

            string sql = "SELECT * FROM Process";
            try
            {
                logger.LogInformation("查询工序");
                var list = processRepository.GetAll(sql, default).ToList();
                if (list != null)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = list;
                    result.Msg = "显示成功";
                }

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("查询工序失败", ex.Message);
                throw;
            }
        }
    }
}
